package com.totoro.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.totoro.domain.Student;
import com.totoro.service.StudentService;
import com.totoro.util.CommonConstant;
import com.totoro.util.Page;

/**
 * 
 * @author zhangpeng
 * 
 */

@Controller
@RequestMapping("/student")
public class StudentController {
	
	@Autowired
	private StudentService studentService;
	
	@RequestMapping("/register")
	public String login(ModelMap modelMap){
		modelMap.addAttribute("student", new Student());
		return "/student/register";
	}
	
	@RequestMapping("/display")
	public ModelAndView displayStudentinfo(){
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/student/display");
		mav.addObject("studentList",studentService.findStudentByAll());
		return mav;
	}
	
	/**
	 * 分页测试方法：根据姓名查询：在display.jsp页面中查询，在findByName.jsp分页显示结果；
	 * 
	 * @param pageNo 页号，点击下一页/上一页类似操作的时候会传入相应的页号，不是必须的；
	 * @param name 查询条件：根据姓名查询，必须在查询页面(findByName.jsp)URL中传入name；
	 */
	@RequestMapping("/findByName")
	public ModelAndView findByName(@RequestParam(value = "pageNo", required = false) Integer pageNo,String name){
		System.out.println(name+":name01==============");
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/student/findByName");
		System.out.println(name+":name02==============");
		mav.addObject("studentName",name);
		//判断页号，
		pageNo = pageNo==null?1:pageNo;
		//返回设置好的分页信息：对应写在findByName.jsp页面中的pageAttrKey属性值中；
		Page pagedPost = studentService.getPagedPosts(name, pageNo,
				CommonConstant.PAGE_SIZE);
		mav.addObject("pagedPost",pagedPost);
		//返回分页查询到的数据记录，对应在findByName.jsp页面中的显示数据位置；
		mav.addObject("result",pagedPost.getResult());
		return mav;
	}
	
	@RequestMapping("/findById/{id}")
	public ModelAndView findStudentById(@PathVariable("id") Integer id){
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/student/update");
		mav.addObject("student",studentService.findStudentById(id));
		return mav;
	}
	
	@RequestMapping("/addStudent")
	public String addStudent(@Validated @ModelAttribute("student") Student student,BindingResult bindingResult){
		if(bindingResult.hasErrors()){
			return "/student/register";
		}else{
			studentService.addStudent(student);
		return "redirect:/student/display.html";
		}
	}
	
	@RequestMapping("/update")
	public String updateStudent(Student student){
		studentService.updateStudent(student);
		return "redirect:/student/display.html";
	}
	
	@RequestMapping("/deleteStudent/{id}")
	public String deleteStudent(Student student){
		studentService.deleteStudent(student);
		return "redirect:/student/display.html";
	}
	
	@RequestMapping("/toBeExcel")
	public String toExcel(ModelMap modelMap){
		modelMap.addAttribute("studentList", studentService.findStudentByAll());
		return "studentExcel";
	}
}










